﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;

namespace appClient
{
	public partial class MyAccount : System.Web.UI.Page
	{

		IClient client;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["user"] != null)
			{
				client = new ChannelFactory<IClient>("clientHttp").CreateChannel();

				if (!Page.IsPostBack)
					fillForm();
			}
			else
			{
				Response.Redirect("Default.aspx");
			}
		}

		private void fillForm()
		{
			if (Session["user"] != null)
			{
				FirstName.Text = ((SerializedClient)Session["user"]).CLIENTFIRSTNAME;
				LastName.Text = ((SerializedClient)Session["user"]).CLIENTLASTNAME;
				Address.Text = ((SerializedClient)Session["user"]).CLIENTADRESS;
				ZIP.Text = ((SerializedClient)Session["user"]).CLIENTZIP;
				City.Text = ((SerializedClient)Session["user"]).CLIENTCITY;
				Phone.Text = ((SerializedClient)Session["user"]).CLIENTPHONE;
				Email.Text = ((SerializedClient)Session["user"]).CLIENTMAIL;
			}
			
			

		}

		protected void CreateUserButton_Click(object sender, EventArgs e)
		{
			if (Session["user"] != null)
			{
				// need to check informations ..

				SerializedClient sCli = new SerializedClient(((SerializedClient)Session["user"]).CLIENTID, LastName.Text, 
															FirstName.Text, Address.Text, ZIP.Text, City.Text, Phone.Text, Email.Text, "France",
															((SerializedClient)Session["user"]).CLIENTPASSWORD);



				/*((appDataContracts.SerializedClient)Session["user"]).CLIENTFIRSTNAME = FirstName.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTLASTNAME = LastName.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTADRESS = Address.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTZIP = ZIP.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTCITY = City.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTPHONE = Phone.Text;
				((appDataContracts.SerializedClient)Session["user"]).CLIENTMAIL = Email.Text;*/
				Session["user"] = sCli;

				bool hasBeenUpdated = client.updateAccountInfo(new SerializedClient(
					((SerializedClient)Session["user"]).CLIENTID, 
					LastName.Text, 
					FirstName.Text, 
					Address.Text, 
					ZIP.Text, 
					City.Text, 
					Phone.Text, 
					Email.Text, 
					"France",
					((SerializedClient)Session["user"]).CLIENTPASSWORD));


			}
		}
	}
}